package com.mogu.biguser.service.impl;

import com.mogu.biguser.common.constant.AuthConst;
import com.mogu.biguser.common.util.ShiroUtils;
import com.mogu.biguser.dao.base_sys.SysAreaMapper;
import com.mogu.biguser.dao.base_sys.SysDeptMapper;
import com.mogu.biguser.dao.biguser.InstrumentInfoMapper;
import com.mogu.biguser.dao.biguser.UserInfoMapper;
import com.mogu.biguser.domain.base_sys.SysArea;
import com.mogu.biguser.domain.base_sys.SysDept;
import com.mogu.biguser.domain.biguser.InstrumentInfo;
import com.mogu.biguser.service.BigUserService;
import com.mogu.biguser.service.dto.ParamValue;
import com.mogu.biguser.service.dto.biguser.BigUser;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class BigUserServiceImpl implements BigUserService {
    private static final Logger logger = LoggerFactory.getLogger(BigUserServiceImpl.class);

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private InstrumentInfoMapper instrumentInfoMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private SysAreaMapper sysAreaMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addBigUser(BigUser user) {
        int i=0;
        try {

             i=    userInfoMapper.insertByBigUser(user);
            List<InstrumentInfo> infoList = user.getInfoList();
            if (infoList!=null&&infoList.size()!=0)
            instrumentInfoMapper.insertByBacth(infoList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            i=-1;
        }
        return i;
    }

    /***
     * 用户类型
     * @return
     */
    @Override
    public List<ParamValue> findListTypes() {
        List<ParamValue> values = new ArrayList<>();
        try {
            String s = "20";
            values = sysDeptMapper.selType(s);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return values;
    }

    @Override
    public List<ParamValue> findListIndustry() {
        List<ParamValue> values = new ArrayList<>();
        try {
            String s = "21";
            values = sysDeptMapper.selType(s);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return values;
    }

    @Override
    public List<ParamValue> findListWaterType() {
        List<ParamValue> values = new ArrayList<>();
        try {
            String s = "23";
            values = sysDeptMapper.selType(s);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return values;}

    @Override
    public String isUserNum(BigUser user) {
        String s ="0";
        try {

        String     values = userInfoMapper.isUserNum(user);
            if(values!=null){
                s ="1";
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            s="-1";
        }

        return s;
    }

    @Override
    public List<BigUser> findListBigUser(BigUser user) {
           List<BigUser> list = new ArrayList<>();

        try {

            list      = userInfoMapper.selListByBigUser(user);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);

        }
        return list;
    }



    @Override
    public int removeBigUser(String list) {
        int i=0;
        try {
            String[] split = list.split(",");
            i = userInfoMapper.deleteByBatch(split);
            for (String s:split){
                instrumentInfoMapper.deleteByUserNum(s);
            }
            //
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            i=-1;
        }
        return i;
    }



    @Override
    public List<ParamValue> findListAvgMonth() {
        List<ParamValue> values = new ArrayList<>();
        try {
            String s = "24";
            values = sysDeptMapper.selType(s);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return values;
    }



    @Override
    public List<ParamValue> findListEnterprise() {
        List<ParamValue> values = new ArrayList<>();
        try {
            String s = "22";
            values = sysDeptMapper.selType(s);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return values;}

    @Override
    public List<SysArea> findListArea() {
        List<SysArea> orderDept = new ArrayList<>();
        List<String> regionList = null;
        try {
            Session session = ShiroUtils.getSession();
            String token = (String) session.getAttribute(AuthConst.TOKEN);
            if (token != null)
                regionList = (List<String>) session.getAttribute(token);
            System.err.println(regionList);
            orderDept = sysAreaMapper.selAreaList(regionList);//查询片区

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }
        return orderDept;
    }

    @Override
    public List<SysDept> findListDept(String areaId) {
        List<SysDept> orderDept = new ArrayList<>();
        List<String> depts = null;
        try {
            Session session = ShiroUtils.getSession();
            depts = (List<String>) session.getAttribute("depts");

            if (depts != null && depts.size() != 0) {
                if (depts.get(0) != null && !"null".equals(depts.get(0))) {
                    orderDept = sysDeptMapper.selDeptList(depts, areaId);//查询部门
                } else {
                    orderDept = sysDeptMapper.selDeptList(null, areaId);
                }
            } else {
                orderDept = sysDeptMapper.selDeptList(null, areaId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
        }

        return orderDept;
    }


}
